<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./dist/vue.js"></script>
</head>
<body>
    <div id="app">
      <ul>
        <li v-for="item of list" :key="item.id"> <!-- 循环 -->
            <input type="checkbox" v-model="item.flag"/> <!-- 双向绑定 -->
            <span >{{item.name}} -- {{item.price}}</span>
            <button @click="item.num -= 1">-</button>{{item.num}}<button  @click="item.num += 1">+</button>
        </li>
      </ul>
      <div>总数：{{totalNum}}</div>
      <div>总价格：{{totalPrice}}</div>
    </div>
</body>
<script>
    const vm = new Vue({ /* new Vue */
      el:"#app",
      data:{
        list:[
          {
            id:1,
            name:'aaa',
            price:1,
            flag:true,
            num:10
          },
          {
            id:2,
            name:'bbb',
            price:5,
            flag:true,
            num:4
          },
          {
            id:1,
            name:'ccc',
            price:6,
            flag:true,
            num:18
          }  
        ]
      },
      computed:{ /* 计算属性 */
        totalNum(){
          return this.list.reduce((sum,item)=>{ /* reduce方法 */
            return item.flag ? sum + item.num :sum + 0
          },0)
        },
        totalPrice(){
            return this.list.reduce((sum,item)=>{
            return item.flag ? sum + item.price * item.num :sum + 0
          },0) 
        }
      }
    })
</script>
</html>